package com.css.fxfzfxqh.modules.economy.controller;

import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.common.rest.model.bo.JcsjDataViewBo;
import com.css.fxfzfxqh.common.rest.model.bo.JcsjVersionMetaInfoBo;
import com.css.fxfzfxqh.common.rest.model.bo.JcsjVersionPageBo;
import com.css.fxfzfxqh.common.rest.model.param.JcDataVersionPageParam;
import com.css.fxfzfxqh.common.rest.model.param.JcsjDataViewParam;
import com.css.fxfzfxqh.common.rest.model.param.JcsjVersionPageParam;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.exceptions.BizException;
import com.css.fxfzfxqh.modules.economy.service.EconomyService;
import com.css.fxfzfxqh.modules.population.mode.params.VersionDataExportParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 经济数据
 */
@RestController
@Slf4j
@RequestMapping("economy")
public class EconomyController {

    @Autowired
    private EconomyService economyService;

    /**
     * 查询人口版本数据列表
     *
     * @param param
     * @return
     */
    @GetMapping("versionPage")
    public RestResponse<?> versionPage(JcsjVersionPageParam param) {
        try {
            JcsjVersionPageBo r = economyService.versionPage(param);
            if (r.getCode() == 200) {
                return RestResponse.succeed(r.getData());
            } else {
                return RestResponse.fail(r.getMessage());
            }
        } catch (BizException e) {
            return RestResponse.fail(e.getMessage());
        } catch (Exception e1) {
            log.error(e1.getMessage(), e1);
            return RestResponse.fail("查询失败");
        }
    }

    /**
     * 查询元数据信息接口
     *
     * @param versionCode 版本号
     */
    @GetMapping("getMetaDataByVersionCode/{versionCode}")
    public RestResponse<?> getMetaDataByVersionCode(@PathVariable("versionCode") String versionCode) {
        try {
            JcsjVersionMetaInfoBo r = economyService.getMetaDataByVersionCode(versionCode);
            if (r.getCode() == 200) {
                return RestResponse.succeed(r.getData());
            } else {
                return RestResponse.fail(r.getMessage());
            }
        } catch (BizException e) {
            return RestResponse.fail(e.getMessage());
        } catch (Exception e1) {
            log.error(e1.getMessage(), e1);
            return RestResponse.fail("查询失败");
        }
    }

    /**
     * 查询数据视图接口
     *
     * @param param 查询参数
     * @return
     * @throws BizException
     */
    @GetMapping("queryDataView")
    public RestResponse<?> queryDataView(JcsjDataViewParam param) {
        try {
            JcsjDataViewBo r = economyService.queryDataView(param);
            if (r.getCode() == 200) {
                return RestResponse.succeed(r.getData());
            } else {
                return RestResponse.fail(r.getMessage());
            }
        } catch (BizException e) {
            return RestResponse.fail(e.getMessage());
        } catch (Exception e1) {
            log.error(e1.getMessage(), e1);
            return RestResponse.fail("查询失败");
        }
    }

    /**
     * 导出版本信息
     *
     * @param response
     * @param params
     * @return
     */
    @PostMapping("versionDataExport")
    public RestResponse versionDataExport(HttpServletResponse response, VersionDataExportParam params) {
        try {
            if (params.getIds() == null) {
                return RestResponse.fail("参数ids不能为空");
            }
            economyService.versionDataExport(params.getIds(), response);
            return null;
        } catch (BizException e) {
            return RestResponse.fail(e.getMessage());
        } catch (Exception e1) {
            log.error(e1.getMessage(), e1);
            return RestResponse.fail("下载失败");
        }
    }

    /**
     * 根据父级code查询子级信息
     *
     * @param code
     * @return
     */
    @GetMapping("getDictItemByDictCode/{code}")
    public RestResponse getChildrenByDictCode(@PathVariable("code") String code) {
        try {
            return RestResponse.succeed(economyService.getDictItemByDictCode(code).getData());
        } catch (BizException e) {
            return RestResponse.fail(e.getMessage());
        } catch (Exception e1) {
            log.error(e1.getMessage(), e1);
            return RestResponse.fail("操作失败");
        }
    }

    @PostMapping("exportDataView")
    public RestResponse<?> exportDataView(HttpServletResponse response, JcDataVersionPageParam param) {
        try {
            economyService.exportDataView(param, response);
            return RestResponse.succeed();
        } catch (BizException e) {
            return RestResponse.fail(e.getMessage());
        } catch (Exception e1) {
            log.error(e1.getMessage(), e1);
            return RestResponse.fail("下载失败");
        }
    }
}
